package com.gettager.tagger.custom;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class PageIndicator extends View {

	
	private int mGap=5;
	private int mRadius = 5;
	private int mPageCount = -1;
	private int mCurrentPage = -1;
	private int mColor=0;

	private Paint mPaint = new Paint(ANTI_ALIAS_FLAG);

	public PageIndicator(Context context) {
		super(context);
	}

	public PageIndicator(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
	}

	public PageIndicator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		if (isInEditMode()) {
			mPageCount = 5;
			mCurrentPage = 0;
		}
		;
	}

	public void setPageCount(int count) {
		this.mPageCount = count;
		invalidate();
	}
	
	public void setIndicatorColor(int color){
		this.mColor = color;
		invalidate();
	}

	public void setCurrentPage(int val) {
		this.mCurrentPage = val;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (mCurrentPage == -1 || mPageCount <= 1) {
			return;
		}

		if (mCurrentPage >= mPageCount) {
			setCurrentPage(mPageCount - 1);
			return;
		}
		mPaint.setColor(mColor);
		mPaint.setStyle(Style.FILL_AND_STROKE);

		int vWidth = getWidth();
		int vHeight = getHeight();		
		
		mRadius = (vHeight/3)-getPaddingBottom()-getPaddingTop();
		mGap=mRadius;
		final int indivWidth = mGap + mRadius + mGap;
		int totalWidth = indivWidth * mPageCount;

		int startX = (vWidth - totalWidth) / 2;
		int startY = vHeight / 2;

		for (int i = 0; i < mPageCount; i++) {
			mPaint.setAlpha((mCurrentPage==i)?255:128);
			int xPos=0;			
			xPos = startX + (indivWidth * i);			
			canvas.drawCircle(xPos, startY, mRadius, mPaint);
		}

	}

}